using Services;
using Services.ObjectPools;
using UnityEngine;

namespace Gobang
{
    public class PlayerManager : MonoBehaviour
    {
        private IObjectManager objectManager;
        private GameManager gameManager;

        public float decisionTime;
        public bool computerFirst;

        private void GeneratePlayer()
        {
            IMyObject obj;
            switch (gameManager.gameMode)
            {
                case EGameMode.WinRate:
                case EGameMode.PVP:
                    obj = objectManager.Activate("HumanPlayer", Vector3.zero, Vector3.zero, transform);
                    obj.Transform.GetComponent<HumanPlayer>().targetChess = 1;
                    obj = objectManager.Activate("HumanPlayer", Vector3.zero, Vector3.zero, transform);
                    obj.Transform.GetComponent<HumanPlayer>().targetChess = 2;
                    break;
                case EGameMode.PVE:
                    obj = objectManager.Activate("HumanPlayer", Vector3.zero, Vector3.zero, transform);
                    obj.Transform.GetComponent<HumanPlayer>().targetChess = computerFirst ? 2 : 1;
                    obj = objectManager.Activate("ComputerPlayer", Vector3.zero, Vector3.zero, transform);
                    obj.Transform.GetComponent<ComputerPlayer>().targetChess = computerFirst ? 1 : 2;
                    obj.Transform.GetComponent<ComputerPlayer>().decisionTime = decisionTime;
                    break;
            }
        }

        private void Awake()
        {
            gameManager = ServiceLocator.Get<GameManager>();
            objectManager = ServiceLocator.Get<IObjectManager>();
            decisionTime = gameManager.decisionTime;
            computerFirst = gameManager.computerFirst;
            GeneratePlayer();
        }
    }
}